﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace IntoTheVoid.Units
{
	public class SingletonBase<T> where T : class, new()
	{
		private static readonly object lockObject = new object();
		private static volatile T instance;

		protected SingletonBase()
		{
			// 防止通过反射创建实例
			if (instance != null)
			{
				throw new InvalidOperationException("Singleton instance already created. Use Instance property to get the instance.");
			}
		}

		public static T Instance
		{
			get
			{
				if (instance == null)
				{
					lock (lockObject)
					{
						if (instance == null)
						{
							instance = new T();
						}
					}
				}
				return instance;
			}
		}
	}
}
